import { createApp } from 'vue'
import './assets/css/style.css'
import App from './App.vue'
import router from './router.js'
import store from './store'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import axios from 'axios'

const app = createApp(App)

for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

//恢复存储的token和user信息
const storedToken = localStorage.getItem('token');
const storedUser = localStorage.getItem('user');

if (storedToken) {
  store.dispatch('saveToken', storedToken);
}

if (storedUser) {
  try{
    const parseUser = JSON.parse(storedUser);
    store.dispatch('saveUser', parseUser);
  }catch(e){
    console.error("Failed to parse stored user:", e);
    localStorage.removeItem('user');
  }
}

// 恢复存储的用户类型
const storedUserType = localStorage.getItem('userType');
if (storedUserType) {
  store.dispatch('saveUserType', storedUserType);
}

app.use(router)
app.use(store)
app.config.globalProperties.$axios = axios
app.mount('#app')
